/**
 * ResumeSend State
 *
 * @date 2020-08-24
 * @author 逗号米
 */

import { GetterTree, MutationTree, ActionTree, Commit, Module } from 'vuex'

import * as http from '@/http/cms/company/interview'
import { isArray, arrayFindIndex } from '@/utils'
import { getPersonalResumeReceiveInterview, getPersonalUserReceiveInterview } from '@/http/cms/company/interview'

// 接口
export interface State {
  currentPage: number,
  perPage: number,
  total: number,
  interviews: Array<any>,
  interview: any,
  resume: any,
  resumeToInterview: any,
  userInterviews: any,

  personalUserInterview: any,
  personalResumeInterview: any,
  companyUserInterview: any,
  companyJobInterview: any
}

// 状态
const state: State = {
  currentPage: 1,
  perPage: 20,
  total: 0,
  interviews: [],
  interview: {},
  resume: {},
  resumeToInterview: {},
  userInterviews: {},
  personalUserInterview: {},
  personalResumeInterview: {},
  companyUserInterview: {},
  companyJobInterview: {}
}

const getters: GetterTree<State, any> = {}

const mutations: MutationTree<State> = {
  /**
   * 分页
   *
   * @param state
   * @param current_page
   * @param page_size
   * @param total
   */
  page(state: State, {
    data_list,
    current_page,
    per_page,
    total
  }: { data_list: any, current_page: number, per_page: number, total: number }): void {
    state.interviews = data_list
    state.currentPage = current_page
    state.perPage = per_page
    state.total = total
  },

  /**
   * 显示
   *
   * @param state
   * @param data
   */
  show(state: State, data: any) {
    state.interview = data
  },

  /**
   * 删除
   *
   * @param state
   * @param ids
   */
  destroy(state: State, ids: Array<number>) {
    if (isArray(ids)) {
      ids.forEach((id) => {

        // 删除主列表
        if (state.interviews.length > 0) {
          const index = arrayFindIndex(state.interviews, 'id', id)
          index >= 0 && state.interviews.splice(index, 1)
        }

        // 删除用户列表
        if (state.userInterviews.data_list.length > 0) {
          const index = arrayFindIndex(state.userInterviews.data_list, 'id', id)
          index >= 0 && state.userInterviews.data_list.splice(index, 1)
        }
      })
    }
  },

  /**
   * 按简历查询投递记录
   *
   * @param state
   * @param data
   */
  setResumeToInterview(state: State, data: any) {
    state.resumeToInterview = data
  },

  /**
   * 用户面试通知
   *
   * @param state
   * @param data
   */
  userInterview(state: State, data: any) {
    state.userInterviews = data
  },

  setCompanyUserInterview(state: State, data: any) {
    state.companyUserInterview = data
  },

  setCompanyJobInterview(state: State, data: any) {
    state.companyJobInterview = data
  },

  setPersonalUserReceiveInterview(state: State, data: any) {
    state.personalUserInterview = data
  },

  setPersonalResumeReceiveInterview(state: State, data: any) {
    state.personalResumeInterview = data
  }
}

const actions: ActionTree<State, any> = {
  /**
   * 首页
   *
   * @param commit
   * @param {Object} payload 负载
   * @return {Promise<any>}
   */
  index({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.index(payload.params).then((res) => {
      if (res.status === 200) {
        commit('page', res.data)
      }
      return res
    })
  },

  /**
   * 查询
   *
   * @param commit
   * @param {Object} [payload] 负载
   * @return {Promise<any>}
   */
  query({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.query(payload.params).then((res) => {
      if (res.status === 200) {
        commit('page', res.data)
      }
      return res
    })
  },

  /**
   * 显示
   *
   * @param commit
   * @param payload
   * @return {Promise<any>}
   */
  show({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.show(payload.id).then((res) => {
      if (res.status === 200) {
        commit('show', res.data)
      }
      return res
    })
  },

  /**
   * 删除
   *
   * @param commit
   * @param payload
   * @return {Promise<any>}
   */
  destroy({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.destroy(payload.id, payload.params).then((res) => {
      if (res.status === 200) {
        commit('destroy', res.data.ids)
      }
      return res
    })
  },
  //
  // /**
  //  * 按简历查询投递记录
  //  *
  //  * @param commit
  //  * @param payload
  //  * @return {Promise<any>}
  //  */
  // resumeReceivedInterview({commit}: { commit: Commit }, payload: any): Promise<any> {
  //   return http.resumeReceivedInterview(payload.id, payload.params).then((res) => {
  //     if (res.status === 200) {
  //       commit('userInterview', res.data)
  //     }
  //     return res
  //   })
  // },
  //
  // /**
  //  * 个人用户收到面试通知
  //  *
  //  * @param commit
  //  * @param payload
  //  * @return {Promise<any>}
  //  */
  // personalUserReceivedInterview({commit}: { commit: Commit }, payload: any): Promise<any> {
  //   return http.personalUserReceivedInterview(payload.uid, payload.params).then((res) => {
  //     if (res.status === 200) {
  //       commit('userInterview', res.data)
  //     }
  //     return res
  //   })
  // },

  /**
   * 企业用户发送面试
   *
   * @param commit
   * @param payload
   */
  getCompanyUserInterview({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.getCompanyUserInterview(payload.uid, payload.params).then((res) => {
      if (res.status === 200) {
        commit('setCompanyUserInterview', res.data)
      }
      return res
    })
  },

  /**
   * 招聘职位发送面试
   *
   * @param commit
   * @param payload
   */
  getCompanyJobInterview({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.getCompanyJobInterview(payload.id, payload.params).then((res) => {
      if (res.status === 200) {
        commit('setCompanyJobInterview', res.data)
      }
      return res
    })
  },

  /**
   * 个人用户收到的面试
   *
   * @param commit
   * @param payload
   */
  getPersonalUserReceiveInterview({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.getPersonalUserReceiveInterview(payload.uid, payload.params).then((res) => {
      if (res.status === 200) {
        commit('setPersonalUserReceiveInterview', res.data)
      }
      return res
    })
  },

  /**
   * 个人简历收到的面试
   *
   * @param commit
   * @param payload
   */
  getPersonalResumeReceiveInterview({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.getPersonalResumeReceiveInterview(payload.id, payload.params).then((res) => {
      if (res.status === 200) {
        commit('setPersonalResumeReceiveInterview', res.data)
      }
      return res
    })
  },
}

export const interview: Module<State, any> = {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}

export default {
  interview
}